package com.hippo.core.ui.compose.setting

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.SimpleTextEdit
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.TimerButton
import com.hippo.core.ui.theme.HippoTheme


@Composable
fun ChangeEmailCodeDialog(
    email: String,
    onSure: (String) -> Unit,
    onDismissRequest: () -> Unit,
    resendCode: (String) -> Unit,
    toHelp: () -> Unit
) {

    BottomSheetDialog(
        title = stringResource(R.string.setting_31),
        onDismissRequest = onDismissRequest
    ) {

        DialogContent(email, onSure, resendCode,toHelp)

    }
}

@Composable
private fun DialogContent(
    email: String,
    onSure: (String) -> Unit,
    resendCode: (String) -> Unit,
    toHelp: () -> Unit
) {

    var code by remember { mutableStateOf("") }


    Column {
        Spacer(modifier = Modifier.height(10.dp))

        Text(
            text = stringResource(R.string.setting_34),
            modifier = Modifier.fillMaxWidth()
        )


        Text(
            text = stringResource(R.string.setting_35, email),
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(modifier = Modifier.height(20.dp))


        SimpleTextEdit(
            modifier = Modifier.fillMaxWidth(),
            value = code,
            onValueChange = {
                code = it
            },
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.Number
            )
        )

        Spacer(modifier = Modifier.height(20.dp))

        Row {

            Spacer(modifier = Modifier.weight(1F))

            Text(
                text = stringResource(R.string.setting_36),
                color = MaterialTheme.colorScheme.primary,
                textDecoration = TextDecoration.Underline,
                modifier = Modifier.click {
                    toHelp()
                }
            )

        }


        Spacer(modifier = Modifier.height(30.dp))

        ThemeButton(
            text = stringResource(R.string.sure),
            modifier = Modifier.fillMaxWidth(),
            onClick = {
                onSure(code)
            }
        )

        Spacer(modifier = Modifier.height(20.dp))

        TimerButton(
            text = stringResource(R.string.setting_39),
            modifier = Modifier.fillMaxWidth(),
            duration = 60,
            onClick = {
                resendCode(email)
            }
        )

        Spacer(modifier = Modifier.height(30.dp))

    }
}


@Preview(showBackground = true)
@Composable
private fun ChangeEmailDialogPreview() {
    HippoTheme {

        DialogContent(
            email = "sss@qq.com",
            onSure = {},
            resendCode = {},
            toHelp = {}
        )
    }

}